package com.kt.uokos.yimo.service.impl;

import com.kt.uokos.yimo.dao.GoodsSpecification;
import com.kt.uokos.yimo.example.GoodsSpecificationExample;
import com.kt.uokos.yimo.mapper.GoodsSpecificationMapper;
import com.kt.uokos.yimo.service.GoodsSpecificationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GoodsSpecificationServiceImpl implements GoodsSpecificationService {

    @Resource
    private GoodsSpecificationMapper goodsSpecificationMapper;

    @Override
    public List<GoodsSpecification> queryGoodsSpecification(Integer id) {
        GoodsSpecificationExample example = new GoodsSpecificationExample();
        example.or().andGoodsIdEqualTo(id).andDeletedEqualTo(false);
        return goodsSpecificationMapper.selectByExample(example);
    }

    @Override
    public Object getSpecificationVoList(Integer id) {
        List<GoodsSpecification> goodsSpecificationList = queryGoodsSpecification(id);

        Map<String, VO> map = new HashMap<>();
        List<VO> specificationVoList = new ArrayList<>();

        for (GoodsSpecification goodsSpecification : goodsSpecificationList) {
            String specification = goodsSpecification.getSpecification();
            VO goodsSpecificationVo = map.get(specification);
            if (goodsSpecificationVo == null) {
                goodsSpecificationVo = new VO();
                goodsSpecificationVo.setName(specification);
                List<GoodsSpecification> valueList = new ArrayList<>();
                valueList.add(goodsSpecification);
                goodsSpecificationVo.setValueList(valueList);
                map.put(specification, goodsSpecificationVo);
                specificationVoList.add(goodsSpecificationVo);
            } else {
                List<GoodsSpecification> valueList = goodsSpecificationVo.getValueList();
                valueList.add(goodsSpecification);
            }
        }
        return specificationVoList;
    }


    private class VO {
        private String name;
        private List<GoodsSpecification> valueList;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public List<GoodsSpecification> getValueList() {
            return valueList;
        }

        public void setValueList(List<GoodsSpecification> valueList) {
            this.valueList = valueList;
        }
    }
}
